<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>RPC系列六之Thrift Transport源码分析 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="简介　　作为一个rpc框架，最基础的功能便是要提供数据传输的功能，所以我们最开始介绍传输层。thrift的传输层实现包括TServerTransport和TTransport，他们都实现了一个接口java.io.Closeable。
TServerTransport　　抽象类TServerTransport有以下两个实现：　　1. TServerSocket：服务端阻塞IO实现，封装了java.n">
<meta property="og:type" content="article">
<meta property="og:title" content="RPC系列六之Thrift Transport源码分析">
<meta property="og:url" content="http://yoursite.com/2015/12/01/rpc/6.RPC系列之Thrift Transport源码分析/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="简介　　作为一个rpc框架，最基础的功能便是要提供数据传输的功能，所以我们最开始介绍传输层。thrift的传输层实现包括TServerTransport和TTransport，他们都实现了一个接口java.io.Closeable。
TServerTransport　　抽象类TServerTransport有以下两个实现：　　1. TServerSocket：服务端阻塞IO实现，封装了java.n">
<meta property="og:updated_time" content="2016-01-29T17:47:36.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="RPC系列六之Thrift Transport源码分析">
<meta name="twitter:description" content="简介　　作为一个rpc框架，最基础的功能便是要提供数据传输的功能，所以我们最开始介绍传输层。thrift的传输层实现包括TServerTransport和TTransport，他们都实现了一个接口java.io.Closeable。
TServerTransport　　抽象类TServerTransport有以下两个实现：　　1. TServerSocket：服务端阻塞IO实现，封装了java.n">
  
    <link rel="alternative" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  <link rel="stylesheet" href="/css/style.css" type="text/css">
  

</head>
<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" results="0" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-rpc/6.RPC系列之Thrift Transport源码分析" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2015/12/01/rpc/6.RPC系列之Thrift Transport源码分析/" class="article-date">
  <time datetime="2015-11-30T22:00:00.000Z" itemprop="datePublished">2015-12-01</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/rpc/">rpc</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      RPC系列六之Thrift Transport源码分析
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="u7B80_u4ECB"><a href="#u7B80_u4ECB" class="headerlink" title="简介"></a>简介</h2><p>　　作为一个rpc框架，最基础的功能便是要提供数据传输的功能，所以我们最开始介绍传输层。thrift的传输层实现包括TServerTransport和TTransport，他们都实现了一个接口java.io.Closeable。</p>
<h2 id="TServerTransport"><a href="#TServerTransport" class="headerlink" title="TServerTransport"></a>TServerTransport</h2><p>　　抽象类TServerTransport有以下两个实现：<br>　　1. TServerSocket：服务端阻塞IO实现，封装了java.net.ServerSocket。直接继承自TServerTransport。<br>　　2. TNonblockingServerSocket：服务端NIO实现，封装了java.nio.channels.ServerSocketChannel和java.nio.channels.Selector等。其继承自TNonblockingServerTransport，而其又继承自TServerTransport。</p>
<h4 id="TServerTransport-1"><a href="#TServerTransport-1" class="headerlink" title="TServerTransport"></a>TServerTransport</h4><p>　　接下来，我们贴出上面几个类的核心代码，首先是TServerTransport：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">TServerTransport</span> <span class="keyword">implements</span> <span class="title">Closeable</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">AbstractServerTransportArgs</span>&lt;<span class="title">T</span> <span class="keyword">extends</span> <span class="title">AbstractServerTransportArgs</span>&lt;<span class="title">T</span>&gt;&gt; </span>&#123;</span><br><span class="line">    <span class="keyword">int</span> backlog = <span class="number">0</span>; <span class="comment">// A value of 0 means the default value will be used (currently set at 50)</span></span><br><span class="line">    <span class="keyword">int</span> clientTimeout = <span class="number">0</span>;</span><br><span class="line">    InetSocketAddress bindAddr;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> T <span class="title">port</span><span class="params">(<span class="keyword">int</span> port)</span> </span>&#123;</span><br><span class="line">      <span class="keyword">this</span>.bindAddr = <span class="keyword">new</span> InetSocketAddress(port);</span><br><span class="line">      <span class="keyword">return</span> (T) <span class="keyword">this</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//省略其他初始化方法</span></span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">final</span> TTransport <span class="title">accept</span><span class="params">()</span> <span class="keyword">throws</span> TTransportException </span>&#123;</span><br><span class="line">    TTransport transport = acceptImpl();</span><br><span class="line">    <span class="keyword">if</span> (transport == <span class="keyword">null</span>) &#123;</span><br><span class="line">      <span class="keyword">throw</span> <span class="keyword">new</span> TTransportException(<span class="string">"accept() may not return NULL"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> transport;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">protected</span> <span class="keyword">abstract</span> TTransport <span class="title">acceptImpl</span><span class="params">()</span> <span class="keyword">throws</span> TTransportException</span>;</span><br><span class="line">  <span class="comment">//省略其他初始化方法</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>　　可以看到，其内部类AbstractServerTransportArgs封装了服务端的参数，如端口，超时等，主要用于初始化参数设置，该思想与构造者（Builder）模式类似。其还有一个非常重要的抽象方法：<strong>acceptImpl</strong>。接下来，我们进入子类TServerSocket看看其对这个方法的实现</p>
<h4 id="TServerSocket"><a href="#TServerSocket" class="headerlink" title="TServerSocket"></a>TServerSocket</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TServerSocket</span> <span class="keyword">extends</span> <span class="title">TServerTransport</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">private</span> ServerSocket serverSocket_ = <span class="keyword">null</span>;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">int</span> clientTimeout_ = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">public</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">ServerSocketTransportArgs</span> </span><br><span class="line">                            <span class="keyword">extends</span> <span class="title">AbstractServerTransportArgs</span>&lt;<span class="title">ServerSocketTransportArgs</span>&gt; </span>&#123;</span><br><span class="line">    ServerSocket serverSocket;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> ServerSocketTransportArgs <span class="title">serverSocket</span><span class="params">(ServerSocket serverSocket)</span> </span>&#123;</span><br><span class="line">      <span class="keyword">this</span>.serverSocket = serverSocket;</span><br><span class="line">      <span class="keyword">return</span> <span class="keyword">this</span>;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">//其他的构造方法省略</span></span><br><span class="line">  </span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="title">TServerSocket</span><span class="params">(ServerSocketTransportArgs args)</span> <span class="keyword">throws</span> TTransportException </span>&#123;</span><br><span class="line">    clientTimeout_ = args.clientTimeout;</span><br><span class="line">    <span class="keyword">if</span> (args.serverSocket != <span class="keyword">null</span>) &#123;</span><br><span class="line">      <span class="keyword">this</span>.serverSocket_ = args.serverSocket;</span><br><span class="line">      <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">      <span class="comment">// Make server socket</span></span><br><span class="line">      serverSocket_ = <span class="keyword">new</span> ServerSocket();</span><br><span class="line">      <span class="comment">// Prevent 2MSL delay problem on server restarts</span></span><br><span class="line">      serverSocket_.setReuseAddress(<span class="keyword">true</span>);</span><br><span class="line">      <span class="comment">// Bind to listening port</span></span><br><span class="line">      serverSocket_.bind(args.bindAddr, args.backlog);</span><br><span class="line">    &#125; <span class="keyword">catch</span> (IOException ioe) &#123;</span><br><span class="line">      close();</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">protected</span> TSocket <span class="title">acceptImpl</span><span class="params">()</span> <span class="keyword">throws</span> TTransportException </span>&#123;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">      Socket result = serverSocket_.accept();</span><br><span class="line">      TSocket result2 = <span class="keyword">new</span> TSocket(result);</span><br><span class="line">      result2.setTimeout(clientTimeout_);</span><br><span class="line">      <span class="keyword">return</span> result2;</span><br><span class="line">    &#125; <span class="keyword">catch</span> (IOException iox) &#123;</span><br><span class="line">      <span class="keyword">throw</span> <span class="keyword">new</span> TTransportException(iox);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  .......</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>　　在上面的类中，我们实现了AbstractServerTransportArgs抽象类和方法acceptImpl。在acceptImpl方法中，每次客户端连接后，我们都要将新的Socket封装到TSocket中并返回，TSocket类请查看后文，这里不做介绍。下面我们看看子类TNonblockingServerSocket对acceptImpl方法的实现</p>
<h4 id="TNonblockingServerSocket"><a href="#TNonblockingServerSocket" class="headerlink" title="TNonblockingServerSocket"></a>TNonblockingServerSocket</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">TNonblockingServerTransport</span> <span class="keyword">extends</span> <span class="title">TServerTransport</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">registerSelector</span><span class="params">(Selector selector)</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TNonblockingServerSocket</span> <span class="keyword">extends</span> <span class="title">TNonblockingServerTransport</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">private</span> ServerSocketChannel serverSocketChannel = <span class="keyword">null</span>;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">private</span> ServerSocket serverSocket_ = <span class="keyword">null</span>;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">int</span> clientTimeout_ = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">public</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">NonblockingAbstractServerSocketArgs</span> <span class="keyword">extends</span></span><br><span class="line">      <span class="title">AbstractServerTransportArgs</span>&lt;<span class="title">NonblockingAbstractServerSocketArgs</span>&gt; </span>&#123;&#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">//其他构造方法省略</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="title">TNonblockingServerSocket</span><span class="params">(NonblockingAbstractServerSocketArgs args)</span> <span class="keyword">throws</span> TTransportException </span>&#123;</span><br><span class="line">    clientTimeout_ = args.clientTimeout;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">      serverSocketChannel = ServerSocketChannel.open();</span><br><span class="line">      serverSocketChannel.configureBlocking(<span class="keyword">false</span>);</span><br><span class="line"></span><br><span class="line">      <span class="comment">// Make server socket</span></span><br><span class="line">      serverSocket_ = serverSocketChannel.socket();</span><br><span class="line">      <span class="comment">// Prevent 2MSL delay problem on server restarts</span></span><br><span class="line">      serverSocket_.setReuseAddress(<span class="keyword">true</span>);</span><br><span class="line">      <span class="comment">// Bind to listening port</span></span><br><span class="line">      serverSocket_.bind(args.bindAddr, args.backlog);</span><br><span class="line">    &#125; <span class="keyword">catch</span> (IOException ioe) &#123;</span><br><span class="line">      serverSocket_ = <span class="keyword">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">protected</span> TNonblockingSocket <span class="title">acceptImpl</span><span class="params">()</span> <span class="keyword">throws</span> TTransportException </span>&#123;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">      SocketChannel socketChannel = serverSocketChannel.accept();</span><br><span class="line">      <span class="keyword">if</span> (socketChannel == <span class="keyword">null</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      TNonblockingSocket tsocket = <span class="keyword">new</span> TNonblockingSocket(socketChannel);</span><br><span class="line">      tsocket.setTimeout(clientTimeout_);</span><br><span class="line">      <span class="keyword">return</span> tsocket;</span><br><span class="line">    &#125; <span class="keyword">catch</span> (IOException iox) &#123;</span><br><span class="line">      <span class="keyword">throw</span> <span class="keyword">new</span> TTransportException(iox);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">registerSelector</span><span class="params">(Selector selector)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">      <span class="comment">// Register the server socket channel, indicating an interest in</span></span><br><span class="line">      <span class="comment">// accepting new connections</span></span><br><span class="line">      serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);</span><br><span class="line">    &#125; <span class="keyword">catch</span> (ClosedChannelException e) &#123;</span><br><span class="line">      <span class="comment">// this shouldn't happen, ideally...</span></span><br><span class="line">      <span class="comment">// <span class="doctag">TODO:</span> decide what to do with this.</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">//省略其他的方法</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>　　我们可以看到，在TNonblockingServerSocket的acceptImpl 方法中，每次客户端的连接，最后都会得到一个java.nio.channels.SocketChannel，并将其封装到TNonblockingSocket中再返回。TNonblockingSocket功能请看下面一节的介绍。</p>
<h2 id="TTransport"><a href="#TTransport" class="headerlink" title="TTransport"></a>TTransport</h2><p>　　在thrift中，抽象类TTransport作为所有数据传输类的基类，其实现主要包括以下几种：<br>　　1. <strong>TSocket</strong>：客户端IO实现，封装了java.net.Socket。继承自TIOStreamTransport，而其又继承自TTransport。<br>　　2. <strong>TNonblockingSocket</strong>：客户端NIO实现，封装了SocketChannel。继承自TNonblockingTransport，而其又继承自TTransport。<br>　　3. <strong>TFramedTransport</strong>：TTransport的一个包裹类，内部维护了一个TTransport对象 <strong>transport_</strong> 。调用TFramedTransport的write方法时，都会先写入TByteArrayOutputStream(ByteArrayOutputStream的一个简单子类)中。调用其flush方法时，先计算出TByteArrayOutputStream中数据的长度，再向transport_写入数据长度，最后写入实际的数据。其实就是一个粘包掉包的处理<br>　　4. <strong>TZlibTransport</strong>：数据压缩传输，其只是在net的输入输出流上包裹了java.util.zip.InflaterInputStream和java.util.zip.DeflaterOutputStream，实现数据的压缩与解压。<br>　　5. <strong>TSaslTransport</strong>：有TSaslClientTransport和TSaslServerTransport的实现。SASL 对用户使用两个重要的标识符。验证 ID (authid) 是用于验证用户的用户 ID。验证 ID 授予用户系统访问权限，授权 ID (userid) 用于检查是否允许用户使用特定选项，这点上与OAuth2.0类似。其主要是对java提供的sasl的封装，几乎用不上，这里我们不做过多的介绍。<br>　　6. <strong>TFileTransport</strong>：文件传输类，允许client将文件传给server，允许server将收到的数据写到文件中。<br>　　7. <strong>THttpClient</strong>：采用http进行rpc调用，其依赖于开源的http组件：httpclient。注意，其服务端的实现为org.apache.thrift.server.TServlet，继承自javax.servlet.http.HttpServlet。<br>　　下面我们选取两个最常用的客户端实现TSocket和TNonblockingSocket进行分析。</p>
<h4 id="TSocket"><a href="#TSocket" class="headerlink" title="TSocket"></a>TSocket</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.net.Socket;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TSocket</span> <span class="keyword">extends</span> <span class="title">TIOStreamTransport</span> </span>&#123;</span><br><span class="line">  <span class="keyword">private</span> Socket socket_ = <span class="keyword">null</span>;</span><br><span class="line">  <span class="keyword">private</span> String host_  = <span class="keyword">null</span>;</span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">int</span> port_ = <span class="number">0</span>;</span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">int</span> socketTimeout_ = <span class="number">0</span>;</span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">int</span> connectTimeout_ = <span class="number">0</span>;</span><br><span class="line">  </span><br><span class="line">  ......</span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="title">TSocket</span><span class="params">(String host, <span class="keyword">int</span> port, <span class="keyword">int</span> socketTimeout, <span class="keyword">int</span> connectTimeout)</span> </span>&#123;</span><br><span class="line">    host_ = host;</span><br><span class="line">    port_ = port;</span><br><span class="line">    socketTimeout_ = socketTimeout;</span><br><span class="line">    connectTimeout_ = connectTimeout;</span><br><span class="line">    initSocket();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">initSocket</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    socket_ = <span class="keyword">new</span> Socket();</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">      socket_.setSoLinger(<span class="keyword">false</span>, <span class="number">0</span>);</span><br><span class="line">      socket_.setTcpNoDelay(<span class="keyword">true</span>);</span><br><span class="line">      socket_.setKeepAlive(<span class="keyword">true</span>);</span><br><span class="line">      socket_.setSoTimeout(socketTimeout_);</span><br><span class="line">    &#125; <span class="keyword">catch</span> (SocketException sx) &#123;</span><br><span class="line">      LOGGER.error(<span class="string">"Could not configure socket."</span>, sx);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isOpen</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (socket_ == <span class="keyword">null</span>) &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> socket_.isConnected();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">open</span><span class="params">()</span> <span class="keyword">throws</span> TTransportException </span>&#123;</span><br><span class="line">    ......</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">      socket_.connect(<span class="keyword">new</span> InetSocketAddress(host_, port_), connectTimeout_);</span><br><span class="line">      inputStream_ = <span class="keyword">new</span> BufferedInputStream(socket_.getInputStream(), <span class="number">1024</span>);</span><br><span class="line">      outputStream_ = <span class="keyword">new</span> BufferedOutputStream(socket_.getOutputStream(), <span class="number">1024</span>);</span><br><span class="line">    &#125; <span class="keyword">catch</span> (IOException iox) &#123;</span><br><span class="line">      close();</span><br><span class="line">      <span class="keyword">throw</span> <span class="keyword">new</span> TTransportException(TTransportException.NOT_OPEN, iox);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  ......</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>　　可以看出，他仅仅是封装了java.net.Socket的初始化和连接的过程，算得上是通用的代码。而其父类TIOStreamTransport只仅仅是进行了基本的io操作read和write，这里不再细究。注意，这里我们采用的是阻塞式的socket。</p>
<h4 id="TNonblockingSocket"><a href="#TNonblockingSocket" class="headerlink" title="TNonblockingSocket"></a>TNonblockingSocket</h4><p>　　这里，我们重点关注TNonblockingSocket和其父类TNonblockingTransport的实现，下面仍然只列出最关键的代码<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">TNonblockingTransport</span> <span class="keyword">extends</span> <span class="title">TTransport</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="keyword">boolean</span> <span class="title">startConnect</span><span class="params">()</span> <span class="keyword">throws</span> IOException</span>;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="keyword">boolean</span> <span class="title">finishConnect</span><span class="params">()</span> <span class="keyword">throws</span> IOException</span>;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> SelectionKey <span class="title">registerSelector</span><span class="params">(Selector selector, <span class="keyword">int</span> interests)</span> <span class="keyword">throws</span> IOException</span>;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">(ByteBuffer buffer)</span> <span class="keyword">throws</span> IOException</span>;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="keyword">int</span> <span class="title">write</span><span class="params">(ByteBuffer buffer)</span> <span class="keyword">throws</span> IOException</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TNonblockingSocket</span> <span class="keyword">extends</span> <span class="title">TNonblockingTransport</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">final</span> SocketAddress socketAddress_;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">final</span> SocketChannel socketChannel_;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">private</span> <span class="title">TNonblockingSocket</span><span class="params">(SocketChannel socketChannel, <span class="keyword">int</span> timeout, SocketAddress socketAddress)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">    socketChannel_ = socketChannel;</span><br><span class="line">    socketAddress_ = socketAddress;</span><br><span class="line"></span><br><span class="line">    socketChannel.configureBlocking(<span class="keyword">false</span>);</span><br><span class="line"></span><br><span class="line">    Socket socket = socketChannel.socket();</span><br><span class="line">    socket.setSoLinger(<span class="keyword">false</span>, <span class="number">0</span>);</span><br><span class="line">    socket.setTcpNoDelay(<span class="keyword">true</span>);</span><br><span class="line">    socket.setKeepAlive(<span class="keyword">true</span>);</span><br><span class="line">    setTimeout(timeout);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> SelectionKey <span class="title">registerSelector</span><span class="params">(Selector selector, <span class="keyword">int</span> interests)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> socketChannel_.register(selector, interests);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setTimeout</span><span class="params">(<span class="keyword">int</span> timeout)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">      socketChannel_.socket().setSoTimeout(timeout);</span><br><span class="line">    &#125; <span class="keyword">catch</span> (SocketException sx) &#123;</span><br><span class="line">      LOGGER.warn(<span class="string">"Could not set socket timeout."</span>, sx);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isOpen</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">// isConnected() does not return false after close(), but isOpen() does</span></span><br><span class="line">    <span class="keyword">return</span> socketChannel_.isOpen() &amp;&amp; socketChannel_.isConnected();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">open</span><span class="params">()</span> <span class="keyword">throws</span> TTransportException </span>&#123;</span><br><span class="line">    <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">"open() is not implemented for TNonblockingSocket"</span>);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">(ByteBuffer buffer)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> socketChannel_.read(buffer);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">(<span class="keyword">byte</span>[] buf, <span class="keyword">int</span> off, <span class="keyword">int</span> len)</span> <span class="keyword">throws</span> TTransportException </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> ((socketChannel_.validOps() &amp; SelectionKey.OP_READ) != SelectionKey.OP_READ) &#123;</span><br><span class="line">      <span class="keyword">throw</span> <span class="keyword">new</span> TTransportException(TTransportException.NOT_OPEN,</span><br><span class="line">        <span class="string">"Cannot read from write-only socket channel"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">      <span class="keyword">return</span> socketChannel_.read(ByteBuffer.wrap(buf, off, len));</span><br><span class="line">    &#125; <span class="keyword">catch</span> (IOException iox) &#123;</span><br><span class="line">      <span class="keyword">throw</span> <span class="keyword">new</span> TTransportException(TTransportException.UNKNOWN, iox);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">write</span><span class="params">(ByteBuffer buffer)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> socketChannel_.write(buffer);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">write</span><span class="params">(<span class="keyword">byte</span>[] buf, <span class="keyword">int</span> off, <span class="keyword">int</span> len)</span> <span class="keyword">throws</span> TTransportException </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> ((socketChannel_.validOps() &amp; SelectionKey.OP_WRITE) != SelectionKey.OP_WRITE) &#123;</span><br><span class="line">      <span class="keyword">throw</span> <span class="keyword">new</span> TTransportException(TTransportException.NOT_OPEN,</span><br><span class="line">        <span class="string">"Cannot write to write-only socket channel"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">      socketChannel_.write(ByteBuffer.wrap(buf, off, len));</span><br><span class="line">    &#125; <span class="keyword">catch</span> (IOException iox) &#123;</span><br><span class="line">      <span class="keyword">throw</span> <span class="keyword">new</span> TTransportException(TTransportException.UNKNOWN, iox);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">flush</span><span class="params">()</span> <span class="keyword">throws</span> TTransportException </span>&#123;</span><br><span class="line">    <span class="comment">// Not supported by SocketChannel.</span></span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">close</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">      socketChannel_.close();</span><br><span class="line">    &#125; <span class="keyword">catch</span> (IOException iox) &#123;</span><br><span class="line">      LOGGER.warn(<span class="string">"Could not close socket."</span>, iox);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">startConnect</span><span class="params">()</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> socketChannel_.connect(socketAddress_);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">finishConnect</span><span class="params">()</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> socketChannel_.finishConnect();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>　　可以看到，这里仍然只是对java nio的一个简单封装，请大家自行查找资料。</p>
<h2 id="u603B_u7ED3"><a href="#u603B_u7ED3" class="headerlink" title="总结"></a>总结</h2><p>　　到这里，我们基本上就分析完Transport了。简单点说，其就是对Java的ServerSocket、Socket、ServerSocketChannel和SocketChannel的一层简单封装，为我们提供最底层的数据传输。那么问题来了，我们传输的数据格式是什么样的呢！只有定义好了数据的传输格式，客户端和服务端才能通解析Transport传递过来的数据。下一篇我们将介绍thrift的数据交换协议TProtocol</p>
<h2 id="u53C2_u8003_u8D44_u6599"><a href="#u53C2_u8003_u8D44_u6599" class="headerlink" title="参考资料"></a>参考资料</h2><p><a href="http://blog.csdn.net/whycold/article/details/8535932" target="_blank" rel="external">http://blog.csdn.net/whycold/article/details/8535932</a><br><a href="http://blog.csdn.net/azhao_dn/article/details/8898610" target="_blank" rel="external">http://blog.csdn.net/azhao_dn/article/details/8898610</a><br><a href="http://blog.csdn.net/azhao_dn/article/details/8899486" target="_blank" rel="external">http://blog.csdn.net/azhao_dn/article/details/8899486</a></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2015/12/01/rpc/6.RPC系列之Thrift Transport源码分析/" data-id="cijzzpv3100251cfskw5v1z4n" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/java/">java</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/rpc/">rpc</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2015/12/01/rpc/7.RPC系列之Thrift TProtocol源码分析/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          RPC系列七之Thrift TProtocol源码分析
        
      </div>
    </a>
  
  
    <a href="/2015/12/01/rpc/5.RPC系列之Thrift入门/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">RPC系列五之Thrift入门</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Categories</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/cache/">cache</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/database/">database</a><span class="category-list-count">13</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/docker/">docker</a><span class="category-list-count">6</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/gis/">gis</a><span class="category-list-count">4</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/git/">git</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/go/">go</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/gradle/">gradle</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/groovy/">groovy</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/hadoop/">hadoop</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/java/">java</a><span class="category-list-count">18</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/linux/">linux</a><span class="category-list-count">4</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/log/">log</a><span class="category-list-count">11</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/mongodb/">mongodb</a><span class="category-list-count">14</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/mq/">mq</a><span class="category-list-count">6</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/mybatis/">mybatis</a><span class="category-list-count">9</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/netty/">netty</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/nginx/">nginx</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/nio/">nio</a><span class="category-list-count">19</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/nodejs/">nodejs</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/redis/">redis</a><span class="category-list-count">6</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/rpc/">rpc</a><span class="category-list-count">14</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/spring/">spring</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/tomcat/">tomcat</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/zookeeper/">zookeeper</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/杂项/">杂项</a><span class="category-list-count">2</span></li></ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/aop/">aop</a><span class="tag-list-count">8</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/aspectj/">aspectj</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/cache/">cache</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/classloader/">classloader</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/common-loggings/">common-loggings</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/commonjs/">commonjs</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/database/">database</a><span class="tag-list-count">10</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/docker/">docker</a><span class="tag-list-count">6</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/dubbo/">dubbo</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/fastdfs/">fastdfs</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/gclib/">gclib</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/geohash/">geohash</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/gis/">gis</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/git/">git</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/go/">go</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/gradle/">gradle</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/groovy/">groovy</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/hadoop/">hadoop</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/hessisan/">hessisan</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/java/">java</a><span class="tag-list-count">48</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/javassist/">javassist</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/kafka/">kafka</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/linux/">linux</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/log/">log</a><span class="tag-list-count">11</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/log4j/">log4j</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/logback/">logback</a><span class="tag-list-count">5</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/map/">map</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/memecached/">memecached</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mina/">mina</a><span class="tag-list-count">5</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mongodb/">mongodb</a><span class="tag-list-count">14</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mq/">mq</a><span class="tag-list-count">6</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mqtt/">mqtt</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mybatis/">mybatis</a><span class="tag-list-count">9</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/netty/">netty</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/nginx/">nginx</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/nio/">nio</a><span class="tag-list-count">5</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/nodejs/">nodejs</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/postgresql/">postgresql</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/protobuf/">protobuf</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/proxy/">proxy</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/push/">push</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/rabbitmq/">rabbitmq</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/redis/">redis</a><span class="tag-list-count">6</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/rpc/">rpc</a><span class="tag-list-count">14</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/sitemesh/">sitemesh</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/sld/">sld</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/slf4j/">slf4j</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/socket/">socket</a><span class="tag-list-count">7</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/spatialite-for-android/">spatialite for android</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/spi/">spi</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/spring/">spring</a><span class="tag-list-count">13</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/tomcat/">tomcat</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/transaction/">transaction</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/web/">web</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/xSocket/">xSocket</a><span class="tag-list-count">7</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/zbus/">zbus</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/zookeeper/">zookeeper</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/数据结构/">数据结构</a><span class="tag-list-count">2</span></li></ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/aop/" style="font-size: 15.38px;">aop</a> <a href="/tags/aspectj/" style="font-size: 11.54px;">aspectj</a> <a href="/tags/cache/" style="font-size: 11.54px;">cache</a> <a href="/tags/classloader/" style="font-size: 10px;">classloader</a> <a href="/tags/common-loggings/" style="font-size: 10px;">common-loggings</a> <a href="/tags/commonjs/" style="font-size: 12.31px;">commonjs</a> <a href="/tags/database/" style="font-size: 16.92px;">database</a> <a href="/tags/docker/" style="font-size: 13.85px;">docker</a> <a href="/tags/dubbo/" style="font-size: 10px;">dubbo</a> <a href="/tags/fastdfs/" style="font-size: 10px;">fastdfs</a> <a href="/tags/gclib/" style="font-size: 10.77px;">gclib</a> <a href="/tags/geohash/" style="font-size: 10px;">geohash</a> <a href="/tags/gis/" style="font-size: 10px;">gis</a> <a href="/tags/git/" style="font-size: 10.77px;">git</a> <a href="/tags/go/" style="font-size: 10px;">go</a> <a href="/tags/gradle/" style="font-size: 10px;">gradle</a> <a href="/tags/groovy/" style="font-size: 10px;">groovy</a> <a href="/tags/hadoop/" style="font-size: 10px;">hadoop</a> <a href="/tags/hessisan/" style="font-size: 11.54px;">hessisan</a> <a href="/tags/java/" style="font-size: 20px;">java</a> <a href="/tags/javassist/" style="font-size: 10px;">javassist</a> <a href="/tags/kafka/" style="font-size: 11.54px;">kafka</a> <a href="/tags/linux/" style="font-size: 12.31px;">linux</a> <a href="/tags/log/" style="font-size: 17.69px;">log</a> <a href="/tags/log4j/" style="font-size: 12.31px;">log4j</a> <a href="/tags/logback/" style="font-size: 13.08px;">logback</a> <a href="/tags/map/" style="font-size: 10px;">map</a> <a href="/tags/memecached/" style="font-size: 10.77px;">memecached</a> <a href="/tags/mina/" style="font-size: 13.08px;">mina</a> <a href="/tags/mongodb/" style="font-size: 19.23px;">mongodb</a> <a href="/tags/mq/" style="font-size: 13.85px;">mq</a> <a href="/tags/mqtt/" style="font-size: 10.77px;">mqtt</a> <a href="/tags/mybatis/" style="font-size: 16.15px;">mybatis</a> <a href="/tags/netty/" style="font-size: 10px;">netty</a> <a href="/tags/nginx/" style="font-size: 11.54px;">nginx</a> <a href="/tags/nio/" style="font-size: 13.08px;">nio</a> <a href="/tags/nodejs/" style="font-size: 10px;">nodejs</a> <a href="/tags/postgresql/" style="font-size: 10.77px;">postgresql</a> <a href="/tags/protobuf/" style="font-size: 10px;">protobuf</a> <a href="/tags/proxy/" style="font-size: 10px;">proxy</a> <a href="/tags/push/" style="font-size: 10px;">push</a> <a href="/tags/rabbitmq/" style="font-size: 11.54px;">rabbitmq</a> <a href="/tags/redis/" style="font-size: 13.85px;">redis</a> <a href="/tags/rpc/" style="font-size: 19.23px;">rpc</a> <a href="/tags/sitemesh/" style="font-size: 10px;">sitemesh</a> <a href="/tags/sld/" style="font-size: 10px;">sld</a> <a href="/tags/slf4j/" style="font-size: 10px;">slf4j</a> <a href="/tags/socket/" style="font-size: 14.62px;">socket</a> <a href="/tags/spatialite-for-android/" style="font-size: 10px;">spatialite for android</a> <a href="/tags/spi/" style="font-size: 10px;">spi</a> <a href="/tags/spring/" style="font-size: 18.46px;">spring</a> <a href="/tags/tomcat/" style="font-size: 10.77px;">tomcat</a> <a href="/tags/transaction/" style="font-size: 10px;">transaction</a> <a href="/tags/web/" style="font-size: 10px;">web</a> <a href="/tags/xSocket/" style="font-size: 14.62px;">xSocket</a> <a href="/tags/zbus/" style="font-size: 10px;">zbus</a> <a href="/tags/zookeeper/" style="font-size: 10.77px;">zookeeper</a> <a href="/tags/数据结构/" style="font-size: 10.77px;">数据结构</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/01/">January 2016</a><span class="archive-list-count">67</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/12/">December 2015</a><span class="archive-list-count">81</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/11/">November 2015</a><span class="archive-list-count">2</span></li></ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recents</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2016/01/30/资料/">(no title)</a>
          </li>
        
          <li>
            <a href="/2016/01/30/java/13.Classloader系列一/">ClassLoader系列一</a>
          </li>
        
          <li>
            <a href="/2016/01/29/hadoop/1.hadoop系列之源码编译/">hadoop系列一之源码编译</a>
          </li>
        
          <li>
            <a href="/2016/01/29/go/1.GO系列之入门/">Go系列一之入门</a>
          </li>
        
          <li>
            <a href="/2016/01/28/tomcat/2.tomcat性能优化/">tomcat性能优化</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2016 John Doe<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="http://libs.baidu.com/jquery/2.0.3/jquery.min.js"></script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css" type="text/css">
  <script src="/fancybox/jquery.fancybox.pack.js" type="text/javascript"></script>


<script src="/js/script.js" type="text/javascript"></script>

  </div>
</body>
</html>